/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.model.PayVersion;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class PayVersionServiceImpl extends AbstractService implements PayVersionService
{
	private Logger logger;
	
	public PayVersionServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
		
	@Override
	public PayVersion getPayVersion(int id) 
	{
		String sql = "select * from payversion where id=" + String.valueOf(id) + " order by createdate desc";
		ModelGenerator<PayVersion> dbcpmg = new ModelGenerator<PayVersion>(PayVersion.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<PayVersion> getPayVersions()
	{
		String sql = "select * from payversion order by createdate desc";
		logger.debug("sql: " + sql);
		ModelGenerator<PayVersion> dbcpmg = new ModelGenerator<PayVersion>(PayVersion.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean savePayVersion(Account operator, String type, PayVersion model)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			model.setId(getTableId("payversion"));
			sql = 
				"insert into payversion(id,versionname,versioncode,remark,createuser,createdate,confirmtemplateid) " +
				"values(" + String.valueOf(model.getId()) + ",'" + encodeDb(model.getVersionname()) + 
				"','" + encodeDb(model.getVersioncode()) + 
				"','" + encodeDb(model.getRemark()) + "','" + model.getCreateuser() + "','" + 
						model.getCreatedate() + "','"+model.getConfirmtemplateid()+"')";
			
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update payversion set remark='" + encodeDb(model.getRemark()) + "'," +
						"confirmtemplateid='"+model.getConfirmtemplateid()+"' " +
				" where id=" + String.valueOf(model.getId()) + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from payversion where id=" + String.valueOf(model.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				PayVersion lo = null;
				if (!type.equals("delete"))
				{
					lo = getPayVersion(model.getId());
				}
				logTableOperate("payversion", type, operator, lo);
				return true;
			}
		}		
		return false;
	}

	@Override
	public List<PayVersion> getPayVersionByVersionCode(String versionCode) {
		String sql = "select * from payversion where versioncode='" + versionCode + "' order by createdate desc";
		logger.debug("sql: " + sql);
		ModelGenerator<PayVersion> dbcpmg = new ModelGenerator<PayVersion>(PayVersion.class);
		return dbcpmg.generateList(dbcp, sql);
	}
}
